<template>
    <rx-dialog @handOk="handleSubmit" @cancel="cancel">
        <rx-layout>
            <div slot="center">
                <a-form :form="form" layout="vertical" style="padding: 12px">
                    <a-form-item style="display:none">
                        <a-input v-decorator="['regId']"/>
                        <a-input v-decorator="['userId',{initialValue:'0'}]"/>
                        <a-input v-decorator="['type',{initialValue:'masking'}]"/>
                    </a-form-item>
                    <a-row :gutter="24">
                        <a-col :span="12">
                            <a-form-item  :label="$ti18('rxForm.name','名称')">
                                <a-input :placeholder="$ti18('rxForm.name','名称')"
                                         @blur.native="getPinyinByName"
                                         v-decorator="['name', {rules: [{required: true, message: $ti18('rxForm.name','名称','','input')}]}]"/>
                            </a-form-item>
                        </a-col>
                        <a-col :span="12">
                            <a-form-item  :label="$ti18('rxForm.rxAlias', '别名')" class="x-a-form-model-error">
                                <a-input :placeholder="$ti18('rxForm.rxAlias', '别名')"
                                         v-decorator="['key', {rules: [
                                             {required: true, message: $ti18('rxForm.rxAlias', '别名','','input')},
                                             {pattern:/^[a-zA-Z][a-zA-Z0-9_-]*$/,message:this.i18n2('form.str','不能以数字开头,不能包含【_-】以外的特殊字符！','input')}
                                             ]}]"/>
                            </a-form-item>
                        </a-col>
                    </a-row>
                    <a-row :gutter="24">
                        <a-col :span="24">
                            <a-form-item  :label="i18n('form.regularEx','正则表达式')">
                                <a-textarea :placeholder="i18n('form.regularEx','正则表达式')"
                                            v-decorator="['regText', {rules: [{required: true, message: i18n('form.regularEx','正则表达式','input')}]}]"
                                            :rows="4"/>
                            </a-form-item>
                        </a-col>
                    </a-row>
                    <a-row v-show="type=='masking'" :gutter="24">
                        <a-col :span="24">
                            <a-form-item  :label="i18n('form.replace','替换公式')">
                                <a-textarea :placeholder="i18n('form.replace','替换公式')"
                                            v-decorator="['mentText', {rules: [{required: true, message: i18n('form.replace','替换公式','input')}]}]"
                                            :rows="4"/>
                            </a-form-item>
                        </a-col>
                    </a-row>
                </a-form>
            </div>
        </rx-layout>
    </rx-dialog>
</template>
<script>
    import FormRegLibApi from '@/api/form/core/formRegLib'
    import {BaseForm, RxDialog, RxLayout} from "@lowcode/jpaas-common-lib";

    export default {
        name: 'FormRegLibEdit',
        mixins: [BaseForm],
        components: {
            RxDialog,
            RxLayout
        },
        data() {
            return {
                type: 'masking'
            }
        },
        methods: {
            i18n(name, text, key) {
                return this.$ti18(name, text, "formRegLib", key);
            },
            i18n2(name,text,key){
                return this.$ti18(name,text,"formBoEntity",key);
            },
            get(id) {
                return FormRegLibApi.get(id);
            },
            save(values) {
                values.appId = this.$parent.curApp.appId;
                return FormRegLibApi.save(values);
            },
            getPinyinByName(){
                let name=this.form.getFieldsValue().name
                if (!name){
                    return;
                }
                if (this.form && this.form.getFieldsValue().key){
                    return;
                }
                this.getPinyin(name).then(res => {
                    //去除不可见的特殊符号
                    if(res){
                        res=res.replace(/\u200B/g,'');
                    }
                    this.form.setFieldsValue({key:res})
                })
            }

        }
    }
</script>
